﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblProductCatDAL:ImtblProductCat
    {
        HKEA_CMSEntities model;
        mtblProductCat cat;
        public mtblProductCatDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblProductCatDAL GetInstance()
        {
            return new mtblProductCatDAL();
        }
        public int InsertENT(mtblProductCat ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblProductCats.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblProductCat> GetListByENT(mtblProductCat ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblProductCats.Where(a => a.ProductCatUID > 0);
            if (ent.ProductCatUID > 0)
                result = result.Where(a => a.ProductCatUID == ent.ProductCatUID);
            if (!string.IsNullOrEmpty(ent.ProductCatID))
                result = result.Where(a=>a.ProductCatID==ent.ProductCatID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<mtblProductCat> GetListByENT(mtblProductCat ent)
        {
            var result = model.mtblProductCats.Where(a => a.ProductCatUID > 0);
            if (ent.ProductCatUID > 0)
                result = result.Where(a => a.ProductCatUID == ent.ProductCatUID);
            if (!string.IsNullOrEmpty(ent.ProductCatID))
                result = result.Where(a => a.ProductCatID.Contains(ent.ProductCatID));//模糊查询
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblProductCat ent)
        {
            var result = model.mtblProductCats.Where(a => a.ProductCatUID > 0);
            if (ent.ProductCatUID > 0)
                result = result.Where(a => a.ProductCatUID == ent.ProductCatUID);
            if (!string.IsNullOrEmpty(ent.ProductCatID))
                result = result.Where(a => a.ProductCatID == ent.ProductCatID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblProductCat ent)
        {
            cat = model.mtblProductCats.Where(a=>a.ProductCatUID==ent.ProductCatUID).FirstOrDefault();
            if (cat != null)
            {
                cat.Deleted = ent.Deleted;
                cat.LastModifyBy = ent.LastModifyBy;
                cat.LastModifyDate = DateTime.Now;
                cat.ProductCat_Chi = ent.ProductCat_Chi;
                cat.ProductCat_Eng = ent.ProductCat_Eng;
                cat.ProductCatID = ent.ProductCatID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblProductCat ent)
        {
            cat = model.mtblProductCats.Where(a => a.ProductCatUID == ent.ProductCatUID).FirstOrDefault();
            if (cat != null)
            {
                model.mtblProductCats.DeleteObject(cat);
            }
            return model.SaveChanges();
        }
    }
}
